<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\admin;
use buwang\base\AdminBaseController;
use buwang\traits\CrudControllerTrait;
use think\facade\View;

/**
 * 第三方平台代码模板库
 */
class MiniappThirdTemplate extends AdminBaseController
{
    use CrudControllerTrait;

    protected $model = null;//模型实例

    protected function initialize()
    {
        parent::initialize();

        $this->model = new \app\common\model\MiniappThirdTemplate();
    }

    /**
     * 查看
     * @menu true
     */
    public function index()
    {
        $page = request()->get('page/d', 1);
        $limit = request()->get('limit/d', 10);

        //TODO 获取搜索条件
        //TODO 如果有不属于表内的字段,查询会报错
        $where = request()->get();
        unset($where['page']);
        unset($where['limit']);
        foreach ($where as $k => $v) {
            if (!$v && $v !== '0' && $v !== 0 && $v !== false) {
                unset($where[$k]);
            }
        }

        try {
            $total = $this->model->where($where)->count();
            $list = $this->model->where($where)->order($this->model->getPk(), 'DESC')->page($page, $limit)->select();
        } catch (\Exception $e) {
            return $this->error('查询失败', ['errorMsg' => $e->getMessage()]);
        }

        $data = compact("total", 'list');
        return $this->success('success', $data);
    }

    /**
     * 详情
     * @menu true
     */
    public function detail()
    {
        $id = request()->get('id');
        if(!$id){
            return $this->error('缺少参数id');
        }
        $row = $this->model->find($id);
        if (!$row) return $this->error('记录不存在');
        return $this->success('success', $row);
    }

    /**
     * 新增
     * @menu true
     */
    public function add()
    {
        if (request()->isPost()) {
            $param = request()->post();

            $this->model->startTrans();
            try {
                //TODO 新增记录
                $this->model->save($param);
                $this->model->commit();
            } catch (\Throwable $e) {
                $this->model->rollback();
                return $this->error('新增失败', ['errorMsg' => $e->getMessage()]);
            }
        }

        return $this->success('success');
    }

    /**
     * 编辑
     * @menu true
     * @param int $id
     */
    public function edit($id = 0)
    {
        $row = $this->model->find($id);
        if (!$row) return $this->error('记录不存在');

        $param = request()->post();
        //TODO 参数验证

        $this->model->startTrans();
        try {
            //TODO 更新记录
            $row->save($param);
            $this->model->commit();
        } catch (\Exception $e) {
            $this->model->rollback();
            return $this->error('编辑失败', ['errorMsg' => $e->getMessage()]);
        }
        return $this->success('编辑成功');
    }

    /**
     * 删除
     * @menu true
     * @param int $ids 主键,删除多行时,拼接
     */
    public function del($ids = 0)
    {
        if (request()->isPost()) {
            if (!$ids) return $this->error('参数有误');

            $pk = $this->model->getPk();
            $list = $this->model->where($pk, 'in', $ids)->select();
            if (!$list) return $this->error('记录不存在');

            $this->model->startTrans();
            try {
                foreach ($list as $item) {
                    $item->delete();
                }

                $this->model->commit();
            } catch (\Exception $e) {
                $this->model->rollback();
                return $this->error('删除失败', ['errorMsg' => $e->getMessage()]);
            }
            return $this->success('success');
        }
    }

}
